package UI.Ejercicio18;

import java.awt.*;
import java.awt.event.*;

import javax.swing.*;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;

public class Main extends JFrame implements MouseListener, ActionListener,
		ChangeListener {
	private JPanel botones;
	private int alto;
	private int ancho;
	private int height;
	private int width;

	JPanel variables;
	JLabel cX;
	JLabel cY;
	JButton borrar;
	JButton cuadrado;
	JButton circulo;
	int coorX;
	int coorY;
	JCheckBox ckCirculos;
	JCheckBox ckCuadrados;
	MiPanel miPanel;
	private JPanel panelB;
	private Graphics g;

	public Main() {

		ventana();
		colocarPaneles();
		this.colocarBotones();
		setVisible(true);
	}

	private void ventana() {
		this.alto = 500;
		this.ancho = 500;
		this.height = Toolkit.getDefaultToolkit().getScreenSize().height;
		this.width = Toolkit.getDefaultToolkit().getScreenSize().width;
		int x = width / 2 - ancho / 2;
		int y = height / 2 - alto / 2;
		setTitle("VENTANA");
		setSize(ancho, alto);
		setLocation(x, y);
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

		addMouseListener(this);

	}

	private void colocarPaneles() {
		setLayout(new BorderLayout());
		miPanel = new MiPanel();
		add(miPanel, BorderLayout.CENTER);
		panelB = new JPanel(new GridLayout(2, 1));
		botones = new JPanel(new GridLayout(2, 1));
		variables = new JPanel(new GridLayout(5, 1));

	}

	private void colocarBotones() {
		cuadrado = new JButton("Cuadrado");
		cuadrado.setActionCommand("cuadrado");
		cuadrado.addActionListener(this);
		circulo = new JButton("Circulo");
		circulo.setActionCommand("circulo");
		circulo.addActionListener(this);
		cX = new JLabel("x");
		cY = new JLabel("y");
		borrar = new JButton("Borrar");
		borrar.setActionCommand("borrar");
		borrar.addActionListener(this);
		add(panelB, BorderLayout.EAST);
		panelB.add(botones);
		panelB.add(variables);
		botones.add(cuadrado);
		botones.add(circulo);
		variables.add(borrar);
		variables.add(cX);
		variables.add(cY);

		// CheckBoxes

		ckCirculos = new JCheckBox("Circulos", true);
		ckCirculos.addChangeListener(this);

		ckCuadrados = new JCheckBox("Cuadrados", true);
		ckCuadrados.addChangeListener(this);

		variables.add(ckCirculos);
		variables.add(ckCuadrados);

	}

	public static void main(String[] args) {
		new Main();
	}

	@Override
	public void mouseClicked(MouseEvent e) {

		this.coorX = e.getX() - 8;
		this.coorY = e.getY() - 30;
		cX.setText("x: " + this.coorX);
		cY.setText("y: " + this.coorY);

	}

	@Override
	public void mouseEntered(MouseEvent arg0) {
		// TODO Auto-generated method stub

	}

	@Override
	public void mouseExited(MouseEvent arg0) {
		// TODO Auto-generated method stub

	}

	@Override
	public void mousePressed(MouseEvent arg0) {
		// TODO Auto-generated method stub

	}

	@Override
	public void mouseReleased(MouseEvent arg0) {
		// TODO Auto-generated method stub

	}

	@Override
	public void actionPerformed(ActionEvent e) {

		switch (e.getActionCommand()) {
		case "cuadrado":
			miPanel.addCuadrado(coorX, coorY);
			miPanel.repaint();
			break;
		case "circulo":
			miPanel.addCirculo(coorX, coorY);
			miPanel.repaint();
			break;
		case "borrar":
			;
			break;
		}
	}

	@Override
	public void stateChanged(ChangeEvent e) {
		miPanel.switchCKCirculos(ckCirculos.isSelected());
		miPanel.switchCKCuadrados(ckCuadrados.isSelected());

	}
}
